'use strict';
/**
 * @description: 查询是否为会员
 */
exports.shorturl = async (ctx) => {
  let { name, type, tile_id, cloud } = ctx.data;
  try {
    const result = await ctx.cloud.topApi.invoke({
      api: 'taobao.miniapp.shorturl.create',
      data: {
        'miniapp_url': `https://m.duanqu.com?_ariver_appid=3000000026610802&query=params=${type},${tile_id}`
      },
      autoSession: true, //如果是云应用中调用，没有这个字段，需要手动传入商家授权获取的sessionkey去调用接口  
    });
    const { result: { model: { short_url } } } = result
    const time = curentTime()
    console.log(time, "---------------------", short_url)
    let sql = `INSERT INTO short (gmt_create,gmt_modified,name,type,tile_id,short_url) VALUES('${time}','${time}','${name}','${type}','${tile_id}','${short_url}');`
    let short = await cloud.dataspace.executeSql(sql, [])
    return short
  } catch (e) { return { success: false, res: e } }
};


//获取东八区时间
var curentTime = function (time) {
  var d = time ? new Date(time) : new Date();
  var localTime = d.getTime();
  var localOffset = d.getTimezoneOffset() * 60000; //获得当地时间偏移的毫秒数
  var utc = localTime + localOffset + 3600000 * 8;
  var now = new Date(utc);

  var year = now.getFullYear(); //年
  var month = now.getMonth() + 1; //月
  var day = now.getDate(); //日
  var hh = now.getHours(); //时
  var mm = now.getMinutes(); //分
  var ss = now.getSeconds(); //秒

  var clock = year + "-";

  if (month < 10)
    clock += "0";

  clock += month + "-";

  if (day < 10)
    clock += "0";

  clock += day + " ";

  if (hh < 10)
    clock += "0";

  clock += hh + ":";

  if (mm < 10)
    clock += "0";

  clock += mm + ":";

  if (ss < 10)
    clock += "0";

  clock += ss;

  return (clock);
}

